package net.assassin8.bautista.stalker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class UploadActivity extends Activity implements Runnable {
	private StalkerProvider sp;
	private String subjRowId;
	private String subjMallId;
	private String subjFirstName;
	private String subjLastName;
	private String subjGender;
	private String subjAge;

	int i;
	private String sessRowId;
	private String sessSubjectId;
	private String sessTrajRowId;
	private String sessTrajTime;
	private String sessTrajSessId;
	private String sessTrajPathX;
	private String sessTrajPathY;
	private String sessTrajShopId;
	private ProgressDialog pd;
	private TextView uploadTextView;
	private String sessTrajFloorPlanId;
	private String subjTime;
	private String sessTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.upload_layout);

		uploadTextView = (TextView) findViewById(R.id.UploadTextView);

		sp = new StalkerProvider(this);

		pd = ProgressDialog.show(this, "Working..", "Uploading data...", true,
				false);

		Thread thread = new Thread(this);
		thread.start();
	}

	@Override
	public void run() {
		sp.open();

		Cursor subjects = sp.getAllSubjects();
		String subjString = "";
		if (subjects.moveToFirst()) {
			for (i = 0; i < subjects.getCount(); i++) {
				subjects.moveToPosition(i);
				subjRowId = subjects.getString(0);
				subjMallId = subjects.getString(1);
				subjFirstName = subjects.getString(2);
				subjLastName = subjects.getString(3);
				subjGender = subjects.getString(4);
				subjAge = subjects.getString(5);
				subjTime = subjects.getString(6);

				// We're going to need to generate a new subject ID when this is
				// uploaded to the server
				subjString += subjRowId + "|" + subjMallId + "|"
						+ subjFirstName + "|" + subjLastName + "|" + subjGender
						+ "|" + subjAge + "|" + subjTime + "<n>";
			}
		}
		subjects.close();

		Cursor sessions = sp.getAllSessions();
		String sessString = "";
		if (sessions.moveToFirst()) {
			for (i = 0; i < sessions.getCount(); i++) {
				sessions.moveToPosition(i);
				sessRowId = sessions.getString(0);
				sessSubjectId = sessions.getString(1);
				sessTime = sessions.getString(2);

				sessString += sessRowId + "|" + sessSubjectId + "|" + sessTime
						+ "<n>";
			}
		}
		sessions.close();

		Cursor sessTrajectories = sp.getAllSessionsTrajectory();
		String sessTrajString = "";
		if (sessTrajectories.moveToFirst()) {
			for (i = 0; i < sessTrajectories.getCount(); i++) {
				sessTrajectories.moveToPosition(i);
				sessTrajRowId = sessTrajectories.getString(0);
				sessTrajTime = sessTrajectories.getString(1);
				sessTrajSessId = sessTrajectories.getString(2);
				sessTrajFloorPlanId = sessTrajectories.getString(3);
				sessTrajPathX = sessTrajectories.getString(4);
				sessTrajPathY = sessTrajectories.getString(5);
				sessTrajShopId = sessTrajectories.getString(6);

				sessTrajString += sessTrajRowId + "|" + sessTrajTime + "|"
						+ sessTrajSessId + "|" + sessTrajFloorPlanId + "|"
						+ sessTrajPathX + "|" + sessTrajPathY + "|"
						+ sessTrajShopId + "<n>";
			}
		}
		sessTrajectories.close();

		HttpRequest.post("http://webstalk.bautista.me/stalker/upload_data",
				"subjects=" + subjString + "&sessions=" + sessString
						+ "&subjects_trajectories=" + sessTrajString);

		// Let's now empty the three tables: subjects, sessions,
		// sessions_trajectory
		sp.emptySubjectsTable();
		sp.emptySessionsTable();
		sp.emptySessionsTrajectoryTable();
		sp.close();
		handler.sendEmptyMessage(0);
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			uploadTextView.setText("Upload successful");

		}
	};
}
